﻿using System.Collections.Generic;
using System.Linq;
using ARR.Entities;
using ARR.UI.CountryService;
using System.Windows.Input;
using ARR.UI.Commands;
using DDF.Entity;
using System;
using ARR.UI.Models;
using ARR.Shared;
using telerik = Telerik.Windows.Controls;
using Telerik.Windows.Controls.GridView;
using System.Collections.ObjectModel;

namespace ARR.UI.ViewModels
{
    class CountryMasterViewModel : WorkspaceViewModel
    {
        private ICountryMasterModel model;
        private ICommand deleteCommand;
        private ICommand saveCommand;
        private ICommand selectionChangedCommand;
        private ICommand rowValidatingCommand;


        public CountryMasterViewModel(ICountryMasterModel model)
        {
            this.model = model;

            this.DisplayName = Labels.MaestroDePaises;

            SelectedCountries = new List<Country>();
        }



        public ICommand DeleteCommand
        {
            get
            {
                if (deleteCommand == null)
                {
                    deleteCommand = new RelayCommand(param => DeleteCountries(), param => SelectedCountries != null);
                }
                return deleteCommand;
            }
        }

        public ICommand SaveCommand
        {
            get
            {
                if (saveCommand == null)
                {
                    saveCommand = new RelayCommand(
                        param => SaveCountries(),
                        param => CanSave());
                }
                return saveCommand;
            }
        }

        public ICommand SelectionChangedCommand
        {
            get
            {
                if (selectionChangedCommand == null)
                {
                    selectionChangedCommand = new RelayCommand(e => GetSelectedItems(e as telerik.SelectionChangeEventArgs));
                }
                return selectionChangedCommand;
            }
        }

        public ICommand AddingNewRowCommand
        {
            get
            {
                if (rowValidatingCommand == null)
                {
                    rowValidatingCommand = new RelayCommand(e => OnAddingNewRow(e as GridViewAddingNewEventArgs));
                }
                return rowValidatingCommand;
            }
        }

        public ObservableCollection<Country> Countries
        {
            get
            {
                return new ObservableCollection<Country>(model.Countries);
            }
        }

        public List<Country> SelectedCountries { get; set; }

        public Array Conventions
        {
            get
            {
                return Enum.GetValues(typeof(Convention));
            }
        }



        
        private void OnAddingNewRow(GridViewAddingNewEventArgs e)
        {
            Country newCountry = new Country();

            model.Countries.Add(newCountry);
            e.NewObject = newCountry;
        }

        private void GetSelectedItems(telerik.SelectionChangeEventArgs e)
        {
            List<Country> added = e.AddedItems.Cast<Country>().ToList();
            List<Country> removed = e.RemovedItems.Cast<Country>().ToList();

            SelectedCountries.AddRange(added);
            removed.ForEach(st => SelectedCountries.Remove(st));
        }

        private void DeleteCountries()
        {
            model.DeleteCountries(SelectedCountries);
        }

        private void SaveCountries()
        {
            if (CanSave())
            {
                model.Save();

                OnPropertyChanged<CountryMasterViewModel>(p => p.Countries);
            }
        }

        private bool CanSave()
        {
            if (Countries.Any(c => c.GetEntityState() != EntityState.Original))
            {
                foreach (Country country in Countries)
                {
                    if (!country.IsValid)
                        return false;
                }

                return true;
            }

            return false;
        }

    }
}
